<?php

/*
 * 	Codificado: 02-Noviembre-2011, 01:05
	Estado: Ajustado_Modelo_Actual(X) Funcionando()
 */

class Recursos_Movilidad_Model extends CI_Model{

	private $table = 'Recursos_Movilidad';

	function _required($required, $data)
	{
		foreach($required as $field)
		if(!isset($data[$field])) return false;

		return true;
	}

	 
	function Insertar($options = array()) {


		$this->db->insert($this->table, $options);
		return $this->db->insert_id();

	}

	function Editar($options = array()) {
		if(!$this->_required(array('IdMovimiento' ,'IdTipoRecurso' ,'Cantidad' ,'Moneda' ),$options))
		return FALSE;

		unset($options['submit'] );

		if(isset($options['IdTipoRecurso']))
		$this->db->set('IdTipoRecurso', $options['IdTipoRecurso']);

		if(isset($options['Cantidad']))
		$this->db->set('Cantidad', $options['Cantidad']);

		if(isset($options['Moneda']))
		$this->db->set('Moneda', $options['Moneda']);

		$this->db->where('IdMovimiento', $options['IdMovimiento']);
		$this->db->where('IdTipoRecurso', $options['IdTipoRecurso']);
		$this->db->update($this->table, $options);

		return $this->db->affected_rows();
	}

	function Eliminar($options = array()) {

		$this->db->where('IdTipoRecurso', $options['IdTipoRecurso']);
		$this->db->where('IdMovimiento', $options['IdMovimiento']);
		$this->db->delete($this->table);

		if($data['error'] = $this->db->_error_message());

		return $data;

	}

	function Ver($options = array()) {

		$this->db->where('IdMovimiento', $options['IdMovimiento']);
		$this->db->where('IdTipoRecurso', $options['IdTipoRecurso']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}

	function Listar($options = array()) {

		$this->db->select('IdMovimiento,IdTipoRecurso,Cantidad,Moneda' );
		if(isset($options['limit']) && isset($options['offset']))
		$this->db->limit($options['limit'], $options['offset']);
		else if(isset($options['limit']))
		$this->db->limit($options['limit']);
			
		$query = $this->db->get($this->table);
		if(isset($options['count']))
		return $query->num_rows();
			
		return $query->result_array();
	}

	function BuscarPorPais($options = array()) {
		if ($this->_required(array('Pais'), $options)) return FALSE ;

		$this->db->where('Pais', $options['Pais']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}

	function BuscarPorMovimiento($options = array()) {

		$this->db->select('IdMovimiento,IdTipoRecurso,
							(SELECT Nombre FROM Recursos WHERE Recursos_Movilidad.IdTipoRecurso = Recursos.Id) AS Tipo, 					
							Cantidad, Moneda' ); 

		$this->db->where('IdMovimiento', $options['IdMovimiento']);

		$query = $this->db->get($this->table);

		return $query->result_array();
	}

	function BuscarPorTipoRecuro($options = array()) {
		if ($this->_required(array('IdTipoRecurso'), $options)) return FALSE ;

		$this->db->where('IdTipoRecurso', $options['IdTipoRecurso']);
		$query = $this->db->get($this->table);

		return $query->result_array();
	}



}
